package vn.conghuy.talkwithme.android.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import vn.conghuy.talkwithme.data.android.ContactModel;
import vn.conghuy.talkwithme.utils.DoubleWrapper;
import vn.conghuy.talkwithme.utils.IntWrapper;
import vn.conghuy.talkwithme.utils.StringWrapper;
import android.content.Context;
import android.database.Cursor;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.widget.Toast;

public class Utils {
	public static void ShowToast(Context context, String text){
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}
	public static void ShowToast(Context context, int textId){
		Toast.makeText(context, textId, Toast.LENGTH_LONG).show();
	}
	
	public static void ReadContactFromDbToList(Context context, ArrayList<ContactModel> listContact)
	{
		listContact.clear();
		Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
		cursor.moveToFirst();
		
		if(cursor.getCount() > 0){
			do{
			   String displayName = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
			   String address = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
			   String sId = cursor.getString(cursor.getColumnIndex(Phone._ID));
			   int id = Integer.parseInt(sId);
			   ContactModel contact = new ContactModel(id, displayName, address);
			   
			   listContact.add(contact);
			}while(cursor.moveToNext());
		}
	}
	
	public static Location myLocation(LocationManager locationMgr, Context context, StringWrapper outLocaitonNameWrapper){
		Location myLocation = null;
		myLocation = locationMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if(myLocation == null)
			myLocation = locationMgr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		
		if(myLocation != null && outLocaitonNameWrapper != null){
			Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
			try {
				List<Address> addresses = geoCoder.getFromLocation(myLocation.getLatitude(), myLocation.getLongitude(), 1);
				String sAddress = "";
				if(addresses.size() > 0){
					for(int i = 0; i < addresses.get(0).getMaxAddressLineIndex() + 1; i++){
						sAddress += addresses.get(0).getAddressLine(i) + "\n";
					}
				}
				// return address
				outLocaitonNameWrapper.setStr(sAddress);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		
		return myLocation;
	}
	
	public static void searchFromName(String locationNameInput, Context context, DoubleWrapper outLatitude, DoubleWrapper outLongtitude, StringWrapper outLocationName){
		Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
		String locationName = null;
		try {
			List<Address> addresses = geoCoder.getFromLocationName(locationNameInput, 1);
			
			if(addresses.size() > 0){
				Address address = addresses.get(0);
				locationName = "";
				for(int i = 0; i < address.getMaxAddressLineIndex() + 1; i++){
					locationName += address.getAddressLine(i) + " - ";
				}
				locationName = locationName.substring(0, locationName.length() - 3);
				
				if(outLatitude != null) outLatitude.setDouble(address.getLatitude());
				if(outLongtitude != null) outLongtitude.setDouble(address.getLongitude());
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if(outLocationName != null)
			outLocationName.setStr(locationName);
	}
	
	public static boolean isInternetConnected(Context context){
		ConnectivityManager connect =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if(connect.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
				|| connect.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED){
			return true;
		}
		return false;
	}
}
